<?php 
/**
 * @version        $Id$
 * @author         master@xuewl.com
 * @copyright      Copyright (c) 2007 - 2014, Chongqing xuewl Information Technology Co., Ltd.
 * @link           http://www.xuewl.com
**/
namespace Special\Model;
use Think\Model;
class SpecialModel extends Model 
{
	protected $_validate = array (
		// array(验证字段1,验证规则,错误提示,[验证条件,附加规则,验证时间]),
		array ('title', 'require', '专题标题不能为空', self::EXISTS_VALIDATE, 'regex', self::MODEL_BOTH),
		array ('title', '', '标题重复！', self::EXISTS_VALIDATE, 'unique', self::MODEL_INSERT),
	);

	protected $_auto = array (
		// array(完成字段1,完成规则,[完成条件,附加规则]),
		array('createtime', NOW_TIME, self::MODEL_BOTH, 'string'),
		array('username', 'getUserName', self::MODEL_BOTH, 'callback'),
		array('userid', 'getUserId', self::MODEL_BOTH, 'callback'),
	);

	public function getUserId() {
		return (int) session('userid');
	}

	public function getUserName() {
		return cookie('admin_username');
	}


	/* 添加或更新数据 */
	public function update($data, $iscreate = TRUE) {		
		if ($iscreate === TRUE) {
			$data = $this->create($data);
		}
		if (empty($data)) {
			$this->error = $this->getError();
			return false;
		}
		if (isset($data['id']) && is_numeric($data['id'])) {
			$result = $this->save($data);
			if (!$result) {
				$this->error = '更新数据失败';
				return false;
			}
		} else {
			$result = $this->add($data);
			if ($result === false) {
				$this->error = '新增数据失败';
				return false;
			}
		}		
		return $result;
	}
	
}